package com.zahb.qadx.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.CommonResponse;
import com.zahb.qadx.model.Org;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.activity.SettingActivity;
import com.zahb.qadx.ui.adapter.EnterListAdapter;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by huwei.
 * Data 2020-10-12.
 * Time 14:41.
 */
public class EnterpriseFragment extends BaseFragmentExt implements View.OnClickListener, EnterListAdapter.onCheckBoxListener {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_save)
    TextView tvSave;
    int orgId;
    String orgName;
    private List<Org> orgList = new ArrayList<>();
    private EnterListAdapter enterListAdapter;
    int id;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
                    showProgressDialog("");
                    addDisposable(RetrofitService.getNetService()
                            .ChangOrg(orgId)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(response -> {
                                hideProgressDialog();
                                getChangeOrgData(response);
                            }, throwable -> {
                                hideProgressDialog();
                                throwable.printStackTrace();
                                Tips.RequestError(getActivity());
                            }));
                }
                break;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_enterprise;
    }

    @Override
    protected void initViews(View view) {
        recyclerView = view.findViewById(R.id.recycler_view);
        tvSave = view.findViewById(R.id.tv_save);
        tvSave.setOnClickListener(this);
        orgList = BaseApplication.getContext().getDataLogin().getOrgList();
        LinearLayoutManager managerGrid = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(managerGrid);
        enterListAdapter = new EnterListAdapter(getActivity(), orgList, BaseApplication.getContext().getDataLogin().getCurrentOrg());
        recyclerView.setAdapter(enterListAdapter);
        enterListAdapter.setOncheckListener(this);
    }

    private void getChangeOrgData(CommonResponse<Object> response) {
        if (response.getStatusCode() == 200) {
            LiveEventBus.get(Constant.KEY_ROOT_ORG_ID_CHANGED, Integer.class).post(enterListAdapter.getSelectId()); //通知首页 任务页刷新了数据
            showBindToast("企业切换成功");
            Intent intent = new Intent(getActivity(), SettingActivity.class);
            intent.putExtra("orgId", orgId);
            intent.putExtra("orgName", orgName);
            intent.putExtra("size", getArguments().getString("size"));
            intent.putExtra("phone", getArguments().getString("phone"));
            startActivity(intent);
            getActivity().finish();
        } else {
            showBindToast(response.getErrorInfo());
        }
    }

    @Override
    public void clickOk(boolean isChecked, int orgUserId,String rootOrgName) {
        orgId = orgUserId;
        orgName = rootOrgName;
        enterListAdapter.notifyDataSetChanged();
    }
}
